﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.DALFactory;

    public class AppRegBLL
    {
        private static readonly IAppSettingDA appSettingDA = DataAccess.CreateAppSettingDA();
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private string currentWhName;
        private List<string> dataBaseID;
        private EnDeCrypt endeCrypt;
        private PubParasInfo pubParasInfo;

        public AppRegBLL()
        {
            this.endeCrypt = new EnDeCrypt();
            this.pubParasInfo = null;
            this.currentWhName = string.Empty;
        }

        public AppRegBLL(string currentWH)
        {
            this.endeCrypt = new EnDeCrypt();
            this.pubParasInfo = null;
            this.currentWhName = string.Empty;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.currentWhName = currentWH;
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public AppRegBLL(PubParasInfo tempPubParasInfo)
        {
            this.endeCrypt = new EnDeCrypt();
            this.pubParasInfo = null;
            this.currentWhName = string.Empty;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.currentWhName = tempPubParasInfo.CurrentWH;
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public bool CheckExpiryDate(string regIDValue)
        {
            DateTime expiryDate = DateTime.Now;
            if (string.IsNullOrEmpty(regIDValue))
            {
                regIDValue = this.ReadRegID();
            }
            if (!string.IsNullOrEmpty(regIDValue))
            {
                regIDValue = this.endeCrypt.DeCrypt(regIDValue);
                expiryDate = DateTime.Parse("20" + Convert.ToInt32(regIDValue.Substring(2, 2), 0x10).ToString() + "-" + Convert.ToInt32(regIDValue.Substring(10, 2), 0x10).ToString() + "-" + Convert.ToInt32(regIDValue.Substring(0x12, 2), 0x10).ToString());
            }
            return (expiryDate >= DateTime.Now);
        }

        public bool CheckMac(string regIDValue)
        {
            string macCode = this.GetMachineCode();
            string macCodeByReg = string.Empty;
            if (string.IsNullOrEmpty(regIDValue))
            {
                regIDValue = this.ReadRegID();
            }
            if (!string.IsNullOrEmpty(regIDValue))
            {
                regIDValue = this.endeCrypt.DeCrypt(regIDValue);
                macCodeByReg = regIDValue.Substring(0, 2) + regIDValue.Substring(4, 2) + regIDValue.Substring(8, 2) + regIDValue.Substring(12, 2) + regIDValue.Substring(0x10, 2) + regIDValue.Substring(20, 2);
            }
            return (string.Compare(macCode, macCodeByReg, true) == 0);
        }

        public AppRegInfo GetAppRegInfo()
        {
            AppRegInfo appRegInfo = new AppRegInfo();
            DataSet ds = commonMethod.GetCurrentUserAndWHCount();
            if ((ds != null) && (ds.Tables.Count > 0))
            {
                appRegInfo.UserCount = int.Parse(ds.Tables[0].Rows[0][0].ToString());
                appRegInfo.WarehouseCount = int.Parse(ds.Tables[0].Rows[0][1].ToString());
            }
            return appRegInfo;
        }

        public string GetMachineCode()
        {
            string serverIP = ConfigurationManager.AppSettings["SERVER_IP"];
            return EnDeCrypt.GetMacAddress(serverIP);
        }

        public string GetMachineCode(string serverIP)
        {
            return EnDeCrypt.GetMacAddress(serverIP);
        }

        public DateTime ReadExpiryDate(string regIDValue)
        {
            DateTime expiryDate = DateTime.Today;
            if (string.IsNullOrEmpty(regIDValue))
            {
                regIDValue = this.ReadRegID();
            }
            if (!string.IsNullOrEmpty(regIDValue))
            {
                regIDValue = this.endeCrypt.DeCrypt(regIDValue);
                expiryDate = DateTime.Parse("20" + Convert.ToInt32(regIDValue.Substring(2, 2), 0x10).ToString() + "-" + Convert.ToInt32(regIDValue.Substring(10, 2), 0x10).ToString() + "-" + Convert.ToInt32(regIDValue.Substring(0x12, 2), 0x10).ToString());
            }
            return expiryDate;
        }

        public string ReadMacCodeReg(string regIDValue)
        {
            string macCodeByReg = string.Empty;
            if (string.IsNullOrEmpty(regIDValue))
            {
                regIDValue = this.ReadRegID();
            }
            if (!string.IsNullOrEmpty(regIDValue))
            {
                regIDValue = this.endeCrypt.DeCrypt(regIDValue);
                macCodeByReg = regIDValue.Substring(0, 2) + regIDValue.Substring(4, 2) + regIDValue.Substring(8, 2) + regIDValue.Substring(12, 2) + regIDValue.Substring(0x10, 2) + regIDValue.Substring(20, 2);
            }
            return macCodeByReg;
        }

        public string ReadRegID()
        {
            AppSettingInfo appSettingInfo = new AppSettingInfo();
            appSettingInfo = appSettingDA.GetAppSettingByID("RegID", this.dataBaseID[0]);
            string regID = string.Empty;
            if (appSettingInfo != null)
            {
                regID = appSettingInfo.Value;
            }
            return regID;
        }

        public int ReadUserLimit(string regIDValue)
        {
            int userLimit = 0;
            if (string.IsNullOrEmpty(regIDValue))
            {
                regIDValue = this.ReadRegID();
            }
            if (!string.IsNullOrEmpty(regIDValue))
            {
                regIDValue = this.endeCrypt.DeCrypt(regIDValue);
                userLimit = Convert.ToInt32(regIDValue.Substring(6, 2) + regIDValue.Substring(14, 1), 0x10);
            }
            return userLimit;
        }

        public int ReadWareHouseLimit(string regIDValue)
        {
            int wareHouseLimit = 0;
            if (string.IsNullOrEmpty(regIDValue))
            {
                regIDValue = this.ReadRegID();
            }
            if (!string.IsNullOrEmpty(regIDValue))
            {
                regIDValue = this.endeCrypt.DeCrypt(regIDValue);
                wareHouseLimit = Convert.ToInt32(regIDValue.Substring(15, 1) + regIDValue.Substring(0x16, 2), 0x10);
            }
            return wareHouseLimit;
        }

        public string UpdateRegID(string regValue)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            AppSettingInfo appSettingInfo = new AppSettingInfo {
                AppSettingID = "RegID",
                Descr = "App RegID",
                Value = regValue,
                IsModifiable = "N",
                CreatedBy = "DBA",
                CreatedDate = DateTime.Now,
                UpdatedBy = "DBA",
                UpdatedDate = DateTime.Now
            };
            string regID = string.Empty;
            try
            {
                if (string.IsNullOrEmpty(regValue))
                {
                    regID = "激活码无效";
                }
                else if (regValue.Length != 0x30)
                {
                    regID = "激活码无效";
                }
                else if (this.ReadExpiryDate(regValue) <= DateTime.Today)
                {
                    regID = "激活码有效期已过!";
                }
                else if (string.Compare(this.ReadMacCodeReg(regValue), this.GetMachineCode(), true) != 0)
                {
                    regID = "激活码无效!";
                }
                else if (this.ReadUserLimit(regValue) <= 0)
                {
                    regID = "激活码无效";
                }
                else if (this.ReadWareHouseLimit(regValue) <= 0)
                {
                    regID = "激活码无效";
                }
                else
                {
                    if (!appSettingDA.CheckAppSettingIDUnique(dataBase, tran, appSettingInfo.AppSettingID, this.dataBaseID[0]))
                    {
                        appSettingDA.InsertAppSetting(dataBase, tran, appSettingInfo, this.dataBaseID);
                    }
                    else
                    {
                        appSettingDA.UpdateAppSetting(dataBase, tran, appSettingInfo, this.dataBaseID);
                    }
                    if (appSettingDA.CheckAppSettingIDUnique(dataBase, tran, "AuthorisedUserNum", this.dataBaseID[0]))
                    {
                        appSettingInfo.AppSettingID = "AuthorisedUserNum";
                        appSettingInfo.Descr = "Authorised User Numbers";
                        appSettingInfo.Value = this.ReadUserLimit(regValue).ToString();
                        appSettingInfo.IsModifiable = "N";
                        appSettingInfo.CreatedBy = "DBA";
                        appSettingInfo.CreatedDate = DateTime.Now;
                        appSettingInfo.UpdatedBy = "DBA";
                        appSettingInfo.UpdatedDate = DateTime.Now;
                        appSettingDA.UpdateAppSetting(dataBase, tran, appSettingInfo, this.dataBaseID);
                    }
                    if (appSettingDA.CheckAppSettingIDUnique(dataBase, tran, "AuthorisedWHNum", this.dataBaseID[0]))
                    {
                        appSettingInfo.AppSettingID = "AuthorisedWHNum";
                        appSettingInfo.Descr = "Authorised Warehouse Numbers";
                        appSettingInfo.Value = this.ReadWareHouseLimit(regValue).ToString();
                        appSettingInfo.IsModifiable = "N";
                        appSettingInfo.CreatedBy = "DBA";
                        appSettingInfo.CreatedDate = DateTime.Now;
                        appSettingInfo.UpdatedBy = "DBA";
                        appSettingInfo.UpdatedDate = DateTime.Now;
                        appSettingDA.UpdateAppSetting(dataBase, tran, appSettingInfo, this.dataBaseID);
                    }
                    regID = string.Format("注册码已更新,有效期{0},用户数{1},仓库数{2}", this.ReadExpiryDate(regValue).ToShortDateString(), this.ReadUserLimit(regValue).ToString(), this.ReadWareHouseLimit(regValue).ToString());
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
                regID = ex.Message;
            }
            finally
            {
                conn.Close();
            }
            return regID;
        }
    }
}

